package com.whjz.dcfs.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Ajax封装类.
 */
public class AjaxUtil {
	
	public static void outputJSONCode(String code ,HttpServletResponse response) throws IOException
	{
		JSONObject json = new JSONObject();
		json.put("code", code);
		json.put("rtnValue", code);
		response.setHeader("ContextType", "text/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
	
	public static void outputJSONResult(Object obj,HttpServletResponse response) throws IOException
	{
		String result = null;
		if((obj instanceof ArrayList) || (obj instanceof LinkedList) || (obj instanceof Vector))
		{
			JSONArray array = JSONArray.fromObject(obj);
			result = array.toString();
		}else
		{
			JSONObject object = JSONObject.fromObject(obj);
			result = object.toString();
		}
		response.setHeader("ContextType", "text/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.write(result, 0, result.length());
		System.out.println("result结果:"+result);
		out.flush();
		out.close();
		
	}
}
